<?php

namespace Modules\Custessa\Helpers;

use CTag;
use CWebUser;
use Modules\Custessa\Helpers\Url;

class Assets {
	/** @var array $css  Array of module css files to be included. */
	static protected $css = [];

	/** @var array $js   Array of module js files to be included. */
	static protected $js = [];

	/**
	 * Add module custom css file.
	 *
	 * @param string $file     Module css file name without ".css" suffix and with prefix with module name and semicolon.
	 * @param string $theme    Theme name if style should be included only for specific Zabbix theme.
	 */
	static public function css($file, $theme = '') {
		if ($theme === '' || getUserTheme(CWebUser::$data) === $theme) {
			static::$css[] = substr($file, -4) === '.css' ? substr($file, 0, -4) : $file;
		}
	}

	/**
	 * Add module custom js file.
	 *
	 * @param string $file     Module js file name without ".js" suffix and with prefix with module name and semicolon.
	 * @param string $theme    Theme name if style should be included only for specific Zabbix theme.
	 */
	static public function js($file, $theme = '') {
		if ($theme === '' || getUserTheme(CWebUser::$data) === $theme) {
			static::$js[] = substr($file, -4) === '.js' ? substr($file, 0, -4) : $file;
		}
	}

	/**
	 * Return custom assets including tag.
	 */
	static public function getHtmlTags(): string {
		$tags_string = '';

		if (static::$css) {
			$files = implode(',', static::$css);

			$tags_string .= (new CTag('link', true))
				->setAttribute('type', 'text/css')
				->setAttribute('rel', 'stylesheet')
				->setAttribute('href', Url::to('custessa.css', ['c' => $files]));
		}

		if (static::$js) {
			$files = implode(',', static::$js);

			$tags_string .= (new CTag('script', true))
				->setAttribute('type', 'text/javascript')
				->setAttribute('src', Url::to('custessa.js', ['c' => $files]));
		}

		return $tags_string;
	}
}
